package com.tao.service.impl.Mq;

import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.UUID;

/**
 * @Author: alubus
 * @Date: 2021/12/3 10:06
 * @Package: com.tao.service.impl.Mq
 */
@Component
public class SubmitSendMqService {
    //    ***********************死信
    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendMessage(@RequestParam String orderNo){
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());// 生成一个消息的唯一id，可不选
        // 声明消息处理器  设置消息的编码以及消息的过期时间  时间毫秒值 为字符串
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            // 设置编码
            messageProperties.setContentEncoding("utf-8");
            // 设置过期时间 一分钟
            int expiration = 1000 * 20;
            messageProperties.setExpiration(String.valueOf(expiration));
            return message;
        };
        // 向ORDER_DL_EXCHANGE 发送消息  形成死信   在OrderQueueReceiver类处理死信交换机转发给转发队列的信息
        rabbitTemplate.convertAndSend("ORDER_DL_EXCHANGE", "DL_KEY", orderNo, messagePostProcessor, correlationData);
        System.out.println(new Date() +  "发送消息，订单号为" + orderNo);
    }


    public void sendSubmitTable(@RequestParam Long sid){
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());// 生成一个消息的唯一id，可不选
        // 声明消息处理器  设置消息的编码以及消息的过期时间  时间毫秒值 为字符串
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            // 设置编码
            messageProperties.setContentEncoding("utf-8");
            // 设置过期时间 一分钟 单位ms
            int expiration = 1000 * 300;
            messageProperties.setExpiration(String.valueOf(expiration));
            return message;
        };
        String strid = String.valueOf(sid);
        // 向ORDER_DL_EXCHANGE 发送消息  形成死信   在OrderQueueReceiver类处理死信交换机转发给转发队列的信息
        rabbitTemplate.convertAndSend("ORDER_DL_EXCHANGE", "DL_KEY",strid, messagePostProcessor, correlationData);
        System.out.println(new Date() +  "订单submitId：" + strid);
    }
}
